<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/3
 * Time: 15:52
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Group extends Base
{
    /**
     * 组别展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $match_id = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_apply_group')
                ->join('up_match_group', 'mg_id = mag_group_id', 'left')
                ->where('mag_match_id', $match_id)
                ->count();// 获取总条数
            $lists = Db::name('match_apply_group')
                ->join('up_match_group', 'mg_id = mag_group_id', 'left')
                ->field('mag_id, mag_apply_num, mg_name, mag_already_num')
                ->where('mag_match_id', $match_id)
                ->page($Nowpage,$limits)
                ->order('mag_id desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加组别
     * @return mixed|\think\response\Json
     */
    public function groupAdd(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->where('match_member_id', session('cm_id'))
            ->field('match_id, match_cate')
            ->find();
        if(Request::instance()->isPost()){
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $mag_group_id = input('mag_group_id');
            $mag_apply_num = input('mag_apply_num');
            $insert = [
                'mag_match_id' => $match['match_id'],
                'mag_group_id' => $mag_group_id,
                'mag_apply_num' => $mag_apply_num
            ];
            $res = Db::table('up_match_apply_group')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $group = Db::table('up_match_group')
            ->where('mg_status', 1)
            ->where('mg_cate', $match['match_cate'])
            ->field('mg_id, mg_name')
            ->select();
        $this->assign([
            'group' => $group
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('group/groupadd');
        }
        
    }
    
    /**
     * 编辑组别
     * @return mixed|\think\response\Json
     */
    public function groupEdit(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->where('match_member_id', session('cm_id'))
            ->field('match_id, match_cate')
            ->find();
        $mag_id = input('mag_id');
        if(Request::instance()->isPost()){
            $mag_group_id = input('mag_group_id');
            $mag_apply_num = input('mag_apply_num');
            $update = [
                'mag_group_id' => $mag_group_id,
                'mag_apply_num' => $mag_apply_num
            ];
            $res = Db::table('up_match_apply_group')->where('mag_id', $mag_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $group = Db::table('up_match_group')
            ->where('mg_status', 1)
            ->where('mg_cate', $match['match_cate'])
            ->field('mg_id, mg_name')
            ->select();
        $match_group = Db::table('up_match_apply_group')
            ->where('mag_id', $mag_id)
            ->field('mag_group_id, mag_apply_num')
            ->find();
        $this->assign([
            'group' => $group,
            'match_group' => $match_group,
            'mag_id' => $mag_id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('group/groupedit');
        }
        
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function groupDel(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $mag_id = input('id');
        $res = Db::table('up_match_apply_group')->delete($mag_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}